package com.yhyvr.api.filter;

import com.yhyvr.api.handle.AuthAnnotation;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.ApiResultCode;
import com.yhyvr.service.ProxyServiceI;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * Created by zhangqin on 2016/12/1.
 * 微商权限认证
 */
public class ApiSpringMethodFilter  implements MethodInterceptor {
    @Resource
    private ProxyServiceI proxyServiceI;
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        Method method = methodInvocation.getMethod();
        if(method.isAnnotationPresent(AuthAnnotation.class)){//加了@LoginMethod注解，被拦截
           ApiReqData apiReqData = (ApiReqData) methodInvocation.getArguments()[0];
            AuthAnnotation authAnnotation = method.getAnnotation(AuthAnnotation.class);
            if(authAnnotation.iLogin()){
                if(apiReqData.cookieIsLogin()) {
                }else {
                    return ApiRespData.buildFailErrorCode(ApiResultCode.FAIL,"10001","请先登录...");
                }
            }
            if(authAnnotation.iBindPhone()){
                if(proxyServiceI.isBindPhone(apiReqData.getCookieUserId())) {
                }else {
                    return ApiRespData.buildFailErrorCode(ApiResultCode.FAIL,"10003","您还没有绑定手机号，请先绑定手机号");
                }
            }
                return methodInvocation.proceed();
        }else{
            return methodInvocation.proceed();
        }
    }
}
